#!/bin/sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/koboldcpp/env_read.sh"

check_app_file_path() {
    local file_name="$1"
    local found=false
    for version in "${APP_VERSION_HISTORY[@]}"; do
        APP_PATH="$LMD_APPS_DIR/koboldcpp/v${version}/${file_name}"
        if [[ -f "$APP_PATH" ]]; then
            echo "Found file: $APP_PATH"
            found=true
            break
        else
            echo "File not found: $APP_PATH"
        fi
    done
    if [[ "$found" == false ]]; then
        echo "No files found with the given versions."
    fi
}

if [ -f /etc/os-release ]; then
    echo "Linux. Not support."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    echo "macOS"
    check_app_file_path $_MAC_INSTALLER_FILE_NAME
    "$APP_PATH"
elif current_os_is_windows; then
    echo "Windows"
    
    
    if command -v nvidia-smi &> /dev/null; then
        echo "Found NVIDIA GPU Driver"
        # echo "Start find App with CUDA 12."
        # source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/check_cuda_ver.sh"
        # if [[ $CUDA_MAJOR_VERSION -eq 12 || $CUDA_MAJOR_VERSION -eq 13 ]]; then
            check_app_file_path $_WINDOWS_INSTALLER_FILE_NAME_CU12
        # fi
    fi

    if [ -z "$APP_PATH" ]; then
        echo "APP_PATH is empty. start find normal version."
        check_app_file_path $_WINDOWS_INSTALLER_FILE_NAME
    fi
  
    "$APP_PATH"
else
    echo "Unknown OS"
fi
